package com.egdd.video.controller;

import com.egdd.video.common.api.CommonResult;
import com.egdd.video.mbg.model.Category;
import com.egdd.video.service.iml.CategoryServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "CategoryController", description = "分类管理")
@Controller
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    CategoryServiceImpl categoryService;

    private static final Logger LOGGER = LoggerFactory.getLogger(CategoryController.class);


    @ApiOperation("获取视频所有分类列表信息")
    @RequestMapping(value = "listAll", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<Category>> getBrandList() {
        return CommonResult.success(categoryService.getAllCategory());
    }

    @ApiOperation("添加分类信息")
    @RequestMapping(value = "insert",method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<Category> insert(@RequestBody Category category){
        int count = categoryService.addCategory(category);
        CommonResult commonResult ;
        if (count == 1) {
            commonResult = CommonResult.success(category);
            LOGGER.debug("insertBrand success:{}", category);
        } else {
            commonResult = CommonResult.failed("操作失败");
            LOGGER.debug("insertBrand failed:{}", category);
        }
        return commonResult;
    }

    @ApiOperation("更新分类信息")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateBrand(@PathVariable("id") Integer id, @RequestBody Category category, BindingResult result) {
        CommonResult commonResult;
        int count = categoryService.updateCategory(id, category);
        if (count == 1) {
            commonResult = CommonResult.success(category);
            LOGGER.debug("updateBrand success:{}", category);
        } else {
            commonResult = CommonResult.failed("操作失败");
            LOGGER.debug("updateBrand failed:{}", category);
        }
        return commonResult;
    }
}
